using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using RaisingStudio.Data;
using RaisingStudio.Data.Providers;
using RaisingStudio.Data.Entities;
using System.Collections;
using System.Data;
using RaisingStudio.Data.Expressions;
using RaisingStudio.Data.Common.Builders;
using RaisingStudio.Data.Common;

namespace UnitTest
{
    [TestFixture]
    public class DataContextTest
    {
        private DataContext dc;
        private List<int> issueList;

        public const string ISSUETITLE_FORTEST = "Issue Title for UnitTest";

        private void CreateDataContext()
        {
            this.dc = new DataContext();
            this.dc.Log = System.Console.Out;
			
			//this.dc.UseDynamicProxy = false;
        }

        private void InsertDummyData()
        {
            this.issueList = new List<int>();
            try
            {
                this.dc.BeginTransaction();
                try
                {
                    Issue issue = new Issue();
                    issue.CreateTime = System.DateTime.Now;
                    issue.Description = "Issue for UnitTest.";
                    issue.IssueType = IssueType.Issue;
                    issue.Locked = false;
                    issue.Owner = "sa";
                    issue.Progress = 10;
                    issue.Status = IssueStatus.New;
                    issue.Title = ISSUETITLE_FORTEST;
                    this.dc.Insert<Issue>(issue);
                    this.issueList.Add(Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID)));

                    Issue issue2 = new Issue();
                    issue2.CreateTime = System.DateTime.Now;
                    issue2.Description = "Issue2 for UnitTest.";
                    issue2.Progress = 20.34M;
                    issue2.Status = IssueStatus.Fixed;
                    issue2.Title = ISSUETITLE_FORTEST;
                    this.dc.Insert<Issue>(issue2);
                    this.issueList.Add(Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID)));

                    this.dc.CommitTransaction();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    this.dc.RollbackTransaction();
                    throw;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
                throw;
            }
        }

        private void ClearDummyData()
        {
            if (this.dc != null)
            {
                try
                {
                    this.dc.BeginTransaction();
                    try
                    {
                        foreach (int issueID in this.issueList)
                        {
                            this.dc.Delete<Issue>(issueID);
                        }

                        this.dc.CommitTransaction();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex);
                        this.dc.RollbackTransaction();
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    throw;
                }
            }
        }

        public DataContextTest()
        {
            CreateDataContext();
        }

        ~DataContextTest()
        {
            this.dc = null;
        }

        [TestFixtureSetUp]
        public void TestCaseSetup()
        {
            InsertDummyData();
        }

        [TestFixtureTearDown]
        public void TestCaseTearDown()
        {
            ClearDummyData();
        }

        [SetUp]
        public void Init()
        {
        }

        [TearDown]
        public void CleanUp()
        {
        }        

        [Test]
        public void Test1()
        {
            Assert.IsNotNull(this.dc);
            Assert.IsNotNull(this.dc.Connection);
            Assert.IsNotNull(this.dc.Database);
            Assert.IsNotNull(this.dc.Provider);
            Assert.IsNotNull(this.dc.LogManager);

            Assert.IsTrue(this.dc.GetCount<Issue>() > 0);
        }

        [Test]
        public void Test2()
        {
            RaisingStudio.Data.CommonCommand select = new CommonCommand();
            select.CommandText = "SELECT * FROM DAC_ISSUE";
            System.Data.DataTable dt1 = this.dc.ExecuteForDataTable(select);
            Assert.IsNotNull(dt1);
            Assert.AreEqual(this.dc.GetCount<Issue>(), dt1.Rows.Count);

            System.Data.DataTable dt2 = this.dc.ExecuteForDataTable(select, 0, 1);
            Assert.IsNotNull(dt2);
            Assert.AreEqual(1, dt2.Rows.Count);

            RaisingStudio.Data.CommonCommand command = new CommonCommand();
            command.CommandText = string.Format("SELECT [{0}], [{1}], [{2}], [{3}] FROM .[{4}]", Issue._.IssueID, Issue._.Title, Issue._.CreateTime, Issue._.Status, Issue._);
            System.Data.DataTable dataTable = this.dc.ExecuteForDataTable<Issue>(command);
            Assert.IsNotNull(dataTable);
            Assert.AreEqual(this.dc.GetCount<Issue>(), dataTable.Rows.Count);

            System.Data.DataTable dataTable2 = this.dc.ExecuteForDataTable<Issue>(command, 0, 1);
            Assert.IsNotNull(dataTable2);
            Assert.AreEqual(1, dataTable2.Rows.Count);

            IList<Issue> issueList = this.dc.ExecuteForList<Issue>(command);
            Assert.IsNotNull(issueList);
            Assert.AreEqual(dataTable.Rows.Count, issueList.Count);
            Assert.IsNotNull(issueList[0]);
            Assert.IsNotNull(issueList[0].Title);

            IList<Issue> issueList2 = this.dc.ExecuteForList<Issue>(command, 0, 1);
            Assert.IsNotNull(issueList2);
            Assert.IsNotEmpty(issueList2 as ICollection);
            Assert.AreEqual(1, issueList2.Count);
        }

        [Test]
        public void Test3()
        {
            int maxID = Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID));
            decimal originalProgress = this.dc.GetData<Issue>(maxID, Issue._.Progress).Progress.Value;
            RaisingStudio.Data.CommonCommand command = new CommonCommand();
            command.CommandText = string.Format("UPDATE DAC_ISSUE SET ISSUE_PRGS = ISSUE_PRGS + 1 WHERE ISSUE_ID = {0}", maxID);
            int result = this.dc.ExecuteNoQuery(command);
            Assert.AreEqual(1, result);
            decimal currentProgress = this.dc.GetData<Issue>(maxID, Issue._.Progress).Progress.Value;
            Assert.AreEqual(originalProgress + 1, currentProgress);

            RaisingStudio.Data.CommonCommand cmd2 = new CommonCommand();
            cmd2.CommandText = string.Format("UPDATE .[{0}] SET [{1}] = [{1}] + 1 WHERE [{2}] = @p1", Issue._, Issue._.Progress, Issue._.IssueID);
            cmd2.AddParameter("@p1", System.Data.DbType.Int32, maxID);
            int result2 = this.dc.ExecuteNoQuery<Issue>(cmd2);
            Assert.AreEqual(1, result2);
            currentProgress = this.dc.GetData<Issue>(maxID, Issue._.Progress).Progress.Value;
            Assert.AreEqual(originalProgress + 2, currentProgress);

            RaisingStudio.Data.CommonCommand cmd3 = new CommonCommand();
            cmd3.CommandText = string.Format("UPDATE .[{0}] SET [{1}] = @p1 WHERE [{2}] = @p2", Issue._, Issue._.Progress, Issue._.IssueID);
            cmd3.AddParameter("@p1", System.Data.DbType.Decimal, originalProgress);
            cmd3.AddParameter("@p2", System.Data.DbType.Int32, maxID);
            int result3 = this.dc.ExecuteNoQuery<Issue>(cmd3);
            Assert.AreEqual(1, result3);
            currentProgress = this.dc.GetData<Issue>(maxID, Issue._.Progress).Progress.Value;
            Assert.AreEqual(originalProgress, currentProgress);
        }

        [Test]
        public void Test4()
        {
            RaisingStudio.Data.CommonCommand cmd = new CommonCommand();
            cmd.CommandText = string.Format("SELECT [{0}] FROM .[{1}] WHERE [{0}] = @p1", Issue._.Title, Issue._);
            cmd.AddParameter("@p1", ISSUETITLE_FORTEST);
            int count = 0;
            foreach (Issue issue in this.dc.ExecuteQuery<Issue>(cmd))
            {
                Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
                count++;
            }
            Assert.GreaterOrEqual(count, 1);

            count = 0;
            foreach (Issue issue in this.dc.ExecuteQuery<Issue>(cmd, 0, 1))
            {
                Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
                count++;
            }
            Assert.AreEqual(1, count);
        }

        [Test]
        public void Test5()
        {
            RaisingStudio.Data.CommonCommand cmd = new CommonCommand();
            cmd.CommandText = "SELECT ISSUE_ID, ISSUE_TITLE FROM DAC_ISSUE WHERE ISSUE_TITLE = @p1";
            cmd.AddParameter("@p1", ISSUETITLE_FORTEST);
            int count = 0;
            IDataReader reader = this.dc.ExecuteReader(cmd);
            try
            {
                while (reader.Read())
                {
                    Assert.AreEqual(ISSUETITLE_FORTEST, reader.GetValue(1));
                    count++;
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            Assert.GreaterOrEqual(count, 1);

            RaisingStudio.Data.CommonCommand cmd2 = new CommonCommand();
            cmd2.CommandText = string.Format("SELECT [{0}] FROM .[{1}] WHERE [{0}] >= @p1", Issue._.Progress, Issue._);
            decimal value = 10;
            cmd2.AddParameter("@p1", DbType.Decimal, value);
            count = 0;
            IDataReader reader2 = this.dc.ExecuteReader<Issue>(cmd2);
            try
            {
                while (reader2.Read())
                {
                    Assert.GreaterOrEqual(Convert.ToDecimal(reader2.GetValue(0)), value);
                    count++;
                }
            }
            finally
            {
                if (reader2 != null)
                {
                    reader2.Close();
                }
            }
            Assert.GreaterOrEqual(count, 1);
        }

        [Test]
        public void Test6()
        {
            RaisingStudio.Data.CommonCommand cmd = new CommonCommand();
            cmd.CommandText = "SELECT MAX(ISSUE_PRGS) FROM DAC_ISSUE";
            object value = this.dc.ExecuteScalar(cmd);
            Assert.AreEqual(this.dc.GetMax<Issue>(Issue._.Progress), value);

            RaisingStudio.Data.CommonCommand cmd2 = new CommonCommand();
            cmd2.CommandText = string.Format("SELECT MAX([{0}]) FROM .[{1}]", Issue._.Progress, Issue._);
            value = this.dc.ExecuteScalar<Issue>(cmd2);
            Assert.AreEqual(this.dc.GetMax<Issue>(Issue._.Progress), value);
        }

        [Test]
        public void Test7()
        {
            Assert.IsTrue(this.dc.Exists<Issue>(this.issueList[0]));
            Assert.IsTrue(this.dc.Exists<Issue>(Issue._.Title == ISSUETITLE_FORTEST));
            Issue issue = new Issue();
            issue.IssueID = this.issueList[0];
            Assert.IsTrue(this.dc.Exists<Issue>(issue));
        }

        [Test]
        public void Test8()
        {
            decimal avg = Convert.ToDecimal(this.dc.GetAvg<Issue>(Issue._.Progress));
            Issue issue = this.dc.GetData<Issue>(this.issueList[0], Issue._.IssueID, Issue._.Progress);
            issue.Progress = issue.Progress.Value + 1;
            this.dc.Update<Issue>(issue, Issue._.IssueID, Issue._.Progress);
            decimal avg2 = Convert.ToDecimal(this.dc.GetAvg<Issue>(Issue._.Progress));
            Assert.GreaterOrEqual(avg2, avg);

            decimal avg3 = Convert.ToDecimal(this.dc.GetAvg<Issue>(Issue._.Progress, Issue._.IssueID == this.issueList[0]));
            Assert.AreEqual(issue.Progress, avg3);
        }

        [Test]
        public void Test9()
        {
            int count = this.dc.GetCount<Issue>();
            Assert.GreaterOrEqual(count, 1);

            count = this.dc.GetCount<Issue>(Issue._.IssueID == this.issueList[0]);
            Assert.AreEqual(1, count);

            count = this.dc.GetCount<Issue>(Issue._.IssueID == this.issueList[0] | Issue._.Title == ISSUETITLE_FORTEST);
            Assert.GreaterOrEqual(count, 1);
        }

        [Test]
        public void Test10()
        {
            Issue issue = this.dc.GetData<Issue>(this.issueList[0]);
            Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
            Assert.AreEqual(this.issueList[0], issue.IssueID);

            Issue issue2 = this.dc.GetData<Issue>(this.issueList[0], Issue._.IssueID, Issue._.Description, Issue._.Status);
            Assert.IsTrue(string.IsNullOrEmpty(issue2.Title));
            Assert.IsTrue(string.IsNullOrEmpty(issue2.Owner));
            Assert.IsFalse(issue2.Progress.HasValue);
            Assert.IsFalse(issue2.Locked.HasValue);

            Issue issue3 = this.dc.GetData<Issue>(this.issueList[0], Issue.Columns);
            Assert.AreEqual(issue.IssueID, issue3.IssueID);
            Assert.AreEqual(issue.Title, issue3.Title);
            Assert.AreEqual(issue.CreateTime, issue3.CreateTime);
            Assert.IsTrue(issue3.Progress.HasValue);

            List<RaisingStudio.Data.Expressions.ColumnExpression> columns = new List<RaisingStudio.Data.Expressions.ColumnExpression>(Issue.Columns);
            columns.Remove(Issue._.Progress);
            Issue issue4 = this.dc.GetData<Issue>(this.issueList[0], columns.ToArray());
            Assert.IsFalse(issue4.Progress.HasValue);
            Assert.IsFalse(string.IsNullOrEmpty(issue4.Title));
            Assert.IsFalse(string.IsNullOrEmpty(issue4.Owner));
            Assert.IsTrue(issue4.Locked.HasValue);

            Issue issue5 = new Issue();
            issue5.IssueID = this.issueList[0];
            Assert.IsTrue(string.IsNullOrEmpty(issue5.Title));
            Assert.IsTrue(string.IsNullOrEmpty(issue5.Owner));
            Assert.IsFalse(issue5.Progress.HasValue);
            Assert.IsFalse(issue5.Locked.HasValue);
            issue5 = this.dc.GetData<Issue>(issue5);
            Assert.IsFalse(string.IsNullOrEmpty(issue5.Title));
            Assert.IsFalse(string.IsNullOrEmpty(issue5.Owner));
            Assert.IsTrue(issue5.Progress.HasValue);
            Assert.IsTrue(issue5.Locked.HasValue);

            Issue issue6 = new Issue();
            issue6.IssueID = this.issueList[0];
            this.dc.GetData<Issue>(issue6);
            Assert.IsFalse(string.IsNullOrEmpty(issue6.Title));
            Assert.IsFalse(string.IsNullOrEmpty(issue6.Owner));
            Assert.IsTrue(issue6.Progress.HasValue);
            Assert.IsTrue(issue6.Locked.HasValue);


            Issue issue7 = new Issue();
            issue7.IssueID = this.issueList[0];
            issue7 = this.dc.GetData<Issue>(issue7, columns.ToArray());
            Assert.IsFalse(string.IsNullOrEmpty(issue7.Title));
            Assert.IsFalse(string.IsNullOrEmpty(issue7.Owner));
            Assert.IsFalse(issue7.Progress.HasValue);
            Assert.IsTrue(issue7.Locked.HasValue);
        }

        [Test]
        public void Test11()
        {
            decimal maxValue = Convert.ToDecimal(this.dc.GetMax<Issue>(Issue._.Progress));
            decimal minValue = Convert.ToDecimal(this.dc.GetMin<Issue>(Issue._.Progress));
            decimal sumValue = Convert.ToDecimal(this.dc.GetSum<Issue>(Issue._.Progress));
            Assert.GreaterOrEqual(maxValue, minValue);
            Assert.GreaterOrEqual(sumValue, maxValue);

            maxValue = Convert.ToDecimal(this.dc.GetMax<Issue>(Issue._.Progress, Issue._.Title == ISSUETITLE_FORTEST));
            minValue = Convert.ToDecimal(this.dc.GetMin<Issue>(Issue._.Progress, Issue._.Title == ISSUETITLE_FORTEST));
            sumValue = Convert.ToDecimal(this.dc.GetSum<Issue>(Issue._.Progress, Issue._.Title == ISSUETITLE_FORTEST));
            Assert.GreaterOrEqual(maxValue, minValue);
            Assert.GreaterOrEqual(sumValue, maxValue);
        }

        [Test]
        public void Test12()
        {
            try
            {
                this.dc.BeginTransaction();
                try
                {
                    int count = this.dc.GetCount<Issue>();

                    Issue issue = new Issue();
                    issue.CreateTime = System.DateTime.Now;
                    issue.Description = "Issue for UnitTest in Test12.";
                    issue.IssueType = IssueType.Issue;
                    issue.Locked = false;
                    issue.Owner = "sa";
                    issue.Progress = 12;
                    issue.Status = IssueStatus.New;
                    issue.Title = string.Format("Issue Test12 - [{0}]", System.Guid.NewGuid());
                    int r1 = this.dc.Insert<Issue>(issue);
                    Assert.AreEqual(1, r1);

                    Assert.AreEqual(count + 1, this.dc.GetCount<Issue>());
                    int issueID = Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID));

                    Issue issue2 = this.dc.GetData<Issue>(issueID);
                    Assert.AreEqual(issue.CreateTime.ToString(), issue2.CreateTime.ToString());
                    Assert.AreEqual(issue.Description, issue2.Description);
                    Assert.AreEqual(issue.IssueType, issue2.IssueType);
                    Assert.AreEqual(issue.Locked, issue2.Locked);
                    Assert.AreEqual(issue.Progress, issue2.Progress);
                    Assert.AreEqual(issue.Status, issue2.Status);
                    Assert.AreEqual(issue.Title, issue2.Title);

                    int result = this.dc.Delete<Issue>(issueID);
                    Assert.AreEqual(1, result);

                    Issue issue3 = new Issue();
                    issue3.Title = string.Format("Issue 3 for Test - [{0}]", System.Guid.NewGuid());
                    issue3.CreateTime = System.DateTime.Now;
                    issue3.Status = IssueStatus.Close;
                    issue3.Progress = 12.34M;

                    issue3.Description = "This description should not be transferred into database.";

                    int r2 = this.dc.Insert<Issue>(issue3, new RaisingStudio.Data.Expressions.ColumnExpression[]{
                        Issue._.Title, Issue._.CreateTime, Issue._.Status, Issue._.Progress});
                    Assert.AreEqual(1, r2);

                    Issue issue4 = this.dc.GetData<Issue>(Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID)));
                    Assert.AreEqual(issue3.Progress, issue4.Progress);
                    Assert.IsFalse(issue4.IssueType.HasValue);
                    Assert.IsFalse(issue4.Locked.HasValue);
                    Assert.AreNotEqual(issue3.Description, issue4.Description);
                    Assert.IsTrue(string.IsNullOrEmpty(issue4.Description));

                    int r3  = this.dc.Delete<Issue>(Issue._.Title == issue3.Title);
                    Assert.AreEqual(1, r3);

                    int currentCount = this.dc.GetCount<Issue>();
                    Assert.AreEqual(count, currentCount);

                    Issue issue5 = new Issue();
                    issue5.Title = "title for issue5";
                    issue5.Status = IssueStatus.New;
                    issue5.CreateTime = System.DateTime.Now;
                    issue5.Progress = 23.45M;
                    int r4 = this.dc.Insert<Issue>(issue5, Issue._.IssueID, Issue._.Title, Issue._.CreateTime, Issue._.Status, Issue._.Progress);
                    Assert.AreEqual(1, r4);

                    currentCount = this.dc.GetCount<Issue>();
                    Assert.AreEqual(count + 1, currentCount);
                    
                    issue5.IssueID = Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID));
                    int r5 = this.dc.Delete<Issue>(issue5);
                    Assert.AreEqual(1, r5);

                    currentCount = this.dc.GetCount<Issue>();
                    Assert.AreEqual(count, currentCount);
                    
                    this.dc.CommitTransaction();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    this.dc.RollbackTransaction();
                    throw;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
                throw;
            }
        }

        [Test]
        public void Test13()
        {
            Issue issue = this.dc.GetData<Issue>(Issue._.Title == ISSUETITLE_FORTEST, "IssueID", "Title");
            Assert.IsNotNull(issue);
            Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
            Assert.IsFalse(issue.IssueType.HasValue);

            CommonCommand cmd = new CommonCommand();
            cmd.CommandText = string.Format("SELECT [IssueID], [{0}] FROM .[Issue] WHERE [{0}] = @p1", Issue._.Title);
            cmd.Parameters.Add("@p1", ISSUETITLE_FORTEST);
            Issue issue2 = this.dc.GetData<Issue>(cmd);
            Assert.IsNotNull(issue2);
            Assert.AreEqual(ISSUETITLE_FORTEST, issue2.Title);
            Assert.IsFalse(issue2.IssueType.HasValue);
        }

        [Test]
        public void Test14()
        {
            int count = 0;
            IEnumerable<Issue> query = this.dc.Query<Issue>();
            int issueID = 0;
            foreach (Issue issue in query)
            {
                Assert.Greater(issue.IssueID, 0);
                Assert.AreNotEqual(issueID, issue.IssueID);
                issueID = issue.IssueID;
                count++;
            }
            Assert.Greater(count, 0);

            count = 0;
            query = this.dc.Query<Issue>(Issue.All.OrderBy(Issue._.IssueID));
            issueID = 0;
            foreach (Issue issue in query)
            {
                Assert.Greater(issue.IssueID, issueID);
                issueID = issue.IssueID;
                count++;
            }
            Assert.Greater(count, 0);

            count = 0;
            query = this.dc.Query<Issue>(Issue.All.OrderBy(Issue._.IssueID, RaisingStudio.Data.Expressions.SortingDirection.Descending));
            foreach (Issue issue in query)
            {
                Assert.LessOrEqual(issue.IssueID, issueID);
                issueID = issue.IssueID;
                count++;
            }
            Assert.Greater(count, 0);

            count = 0;
            query = this.dc.Query<Issue>(Issue.All.Where(Issue._.Title == ISSUETITLE_FORTEST & Issue._.IssueID != 0) ^ Issue._.IssueID, Issue._.IssueID, Issue._.Title);
            issueID = 0;
            foreach (Issue issue in query)
            {
                Assert.Greater(issue.IssueID, issueID);
                Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
                Assert.IsTrue(string.IsNullOrEmpty(issue.Description));
                Assert.IsFalse(issue.Progress.HasValue);
                Assert.IsFalse(issue.Locked.HasValue);
                issueID = issue.IssueID;
                count++;
            }
            Assert.Greater(count, 0);

            count = 0;
            query = this.dc.Query<Issue>(Issue._.Title == ISSUETITLE_FORTEST ^ !Issue._.IssueID, 0, 1);
            foreach (Issue issue in query)
            {
                Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
                count++;
            }
            Assert.AreEqual(1, count);

            query = this.dc.Query<Issue>((Issue._.Title == ISSUETITLE_FORTEST & Issue._.IssueID >= 0) ^ !Issue._.IssueID ^ Issue._.Title, 0, 1, Issue._.IssueID, Issue._.Title);
            count = 0;
            foreach (Issue issue in query)
            {
                Assert.Greater(issue.IssueID, 0);
                Assert.AreEqual(ISSUETITLE_FORTEST, issue.Title);
                Assert.IsTrue(string.IsNullOrEmpty(issue.Description));
                Assert.IsFalse(issue.Progress.HasValue);
                Assert.IsFalse(issue.Locked.HasValue);
                count++;
            }
            Assert.AreEqual(1, count);
        }

        [Test]
        public void Test15()
        {
            System.Data.DataTable dataTable = this.dc.QueryForDataTable<Issue>();
            Assert.IsNotNull(dataTable);
            Assert.Greater(dataTable.Rows.Count, 1);

            dataTable = this.dc.QueryForDataTable<Issue>(Issue._.Title == ISSUETITLE_FORTEST);
            Assert.IsNotNull(dataTable);
            Assert.Greater(dataTable.Rows.Count, 0);

            dataTable = this.dc.QueryForDataTable<Issue>(Issue._.Title == ISSUETITLE_FORTEST & Issue._.IssueID > 0, Issue._.IssueID, Issue._.Title, Issue._.Status, Issue._.CreateTime);
            Assert.IsNotNull(dataTable);
            Assert.Greater(dataTable.Rows.Count, 0);
            Assert.AreEqual(ISSUETITLE_FORTEST, dataTable.Rows[0][(string)Issue._.Title]);
            Assert.IsTrue(dataTable.Rows[0].IsNull((string)Issue._.Progress));

            dataTable = this.dc.QueryForDataTable<Issue>(Issue._.Title == ISSUETITLE_FORTEST, 0, 1, Issue._.IssueID, Issue._.Title, Issue._.Status, Issue._.CreateTime);
            Assert.IsNotNull(dataTable);
            Assert.AreEqual(1, dataTable.Rows.Count);
            Assert.AreEqual(ISSUETITLE_FORTEST, dataTable.Rows[0][(string)Issue._.Title]);
            Assert.IsTrue(dataTable.Rows[0].IsNull((string)Issue._.Progress));
        }

        [Test]
        public void Test16()
        {
            IList<Issue> issueList = this.dc.QueryForList<Issue>();
            Assert.IsNotNull(issueList);
            Assert.IsNotEmpty(issueList as ICollection);

            issueList = this.dc.QueryForList<Issue>(Issue._.Title == ISSUETITLE_FORTEST & Issue._.IssueID > 0);
            Assert.IsNotNull(issueList);
            issueList = this.dc.QueryForList<Issue>(Issue._.Title == ISSUETITLE_FORTEST, Issue._.IssueID, Issue._.Title);
            Assert.IsNotNull(issueList);
            issueList = this.dc.QueryForList<Issue>(Issue._.Title == ISSUETITLE_FORTEST, 0, 1, Issue._.IssueID, Issue._.Title);
            Assert.IsNotNull(issueList);
            Assert.AreEqual(1, issueList.Count);
            Assert.AreEqual(ISSUETITLE_FORTEST, issueList[0].Title);
            Assert.IsFalse(issueList[0].Progress.HasValue);
        }

        [Test]
        public void Test17()
        {
            Issue issue = new Issue();
            issue.CreateTime = System.DateTime.Now;
            issue.Status = IssueStatus.New;
            issue.Title = "Title for Test17";
            int result = this.dc.Save<Issue>(issue);

            Assert.AreEqual(1, result);
            int issueID = Convert.ToInt32(this.dc.GetMax<Issue>(Issue._.IssueID));
            Issue issue2 = this.dc.GetData<Issue>(issueID);
            Assert.AreEqual(issue.Title, issue2.Title);
            Assert.IsFalse(issue2.Progress.HasValue);

            issue2.Progress = 17.89M;
            result = this.dc.Save<Issue>(issue2, Issue._.Progress);
            Assert.AreEqual(1, result);

            Issue issue3 = this.dc.GetData<Issue>(issueID);
            Assert.AreEqual(issue2.Progress, issue3.Progress);
            Assert.IsFalse(issue3.IssueType.HasValue);

            issue3.Progress = 78.90M;
            issue3.IssueType = IssueType.Issue;
            result = this.dc.Update<Issue>(issue3, Issue._.IssueType);
            Assert.AreEqual(1, result);

            Issue issue4 = this.dc.GetData<Issue>(issueID);
            Assert.AreEqual(IssueType.Issue, issue4.IssueType.Value);
            Assert.AreNotEqual(issue3.Progress, issue4.Progress);
            Assert.AreEqual(issue2.Progress, issue4.Progress);

            result = this.dc.Delete<Issue>(issue4);
            Assert.AreEqual(1, result);
        }

        [Test]
        public void Test18()
        {
            System.Data.DataTable dataTable = this.dc.QueryForDataTable<Issue>();
            System.Data.DataRow dataRow = dataTable.Rows.Find(this.issueList[0]);
            decimal progress = Convert.ToDecimal(dataRow[(string)Issue._.Progress]);
            dataRow[(string)Issue._.Progress] = progress + 1;
            int result = this.dc.UpdateDataTable<Issue>(dataTable);

            Assert.AreEqual(1, result);
            Issue issue = this.dc.GetData<Issue>(this.issueList[0]);
            Assert.AreNotEqual(progress, issue.Progress.Value);
            Assert.AreEqual(progress + 1, issue.Progress.Value);

            result = this.dc.UpdateDataTable<Issue>(dataTable);
            Assert.AreEqual(0, result);

            dataRow[(string)Issue._.Progress] = progress;
            result = this.dc.UpdateDataTable<Issue>(dataTable, Issue._.Progress);
            Assert.AreEqual(1, result);

            issue = this.dc.GetData<Issue>(this.issueList[0]);
            Assert.AreEqual(progress, issue.Progress);
        }

        [Test]
        public void Test19()
        {
            int issueID = this.issueList[0];
            Issue issue = this.dc.GetData<Issue>(issueID);
            decimal progress = issue.Progress.Value;
            issue.Progress = progress + 1;

            int result = this.dc.Update<Issue>(issue, Issue._.IssueID == issueID, Issue._.Progress);
            Assert.AreEqual(1, result);

            Issue issue2 = this.dc.GetData<Issue>(issueID);
            Assert.AreEqual(issue.Progress, issue2.Progress);
            Assert.AreNotEqual(progress, issue2.Progress);

            issue.Description = "Issue description for Test19.";
            result = this.dc.Update<Issue>(issue, Issue._.IssueID.In(this.issueList.ToArray()), Issue._.Progress, Issue._.Description);

            Assert.GreaterOrEqual(result, 2);
            foreach (Issue it in this.dc.Query<Issue>(Issue._.IssueID.In(this.issueList)))
            {
                Assert.AreEqual(issue.Progress, it.Progress);
                Assert.AreEqual(issue.Description, it.Description);
            }            
        }

        [Test]
        public void Test20()
        {
            System.Data.DataTable dataTable = this.dc.CreateDataTable<Issue>();
            Assert.IsNotNull(dataTable);
            Assert.AreEqual(Issue.Columns.Length, dataTable.Columns.Count);

            dataTable = this.dc.CreateDataTable<Issue>(Issue._.IssueID, Issue._.Title);
            Assert.IsNotNull(dataTable);
            Assert.AreEqual(2, dataTable.Columns.Count);

            int count = this.dc.GetCount<Issue>(Issue._.IssueID.Between(0, this.issueList[this.issueList.Count - 1]));
            int result = this.dc.FillDataTable<Issue>(dataTable, Issue._.IssueID.Between(0, this.issueList[this.issueList.Count - 1]), count - 2, this.issueList.Count, Issue._.IssueID, Issue._.Title);
            Assert.AreEqual(this.issueList.Count, result);
            System.Data.DataRow dataRow = dataTable.Rows.Find(this.issueList[0]);
            Assert.IsNotNull(dataRow);
            Assert.AreEqual(ISSUETITLE_FORTEST, dataRow[(string)Issue._.Title]);
        }

        [Test]
        public void Test21()
        {
            string userID = System.Guid.NewGuid().ToString();

            SystemUser user = new SystemUser();
            user.UserID = userID;
            user.UserName = "user name";
            user.FullName = "full name";
            user.ChangingPassword = false;
            user.UserDescription = "user description";

            int result = this.dc.Save<SystemUser>(user);
            Assert.AreEqual(1, result);

            result = this.dc.Update<SystemUser>(userID, user);
            result = this.dc.Update<SystemUser>(userID, user, SystemUser.Columns);
            
            List<ColumnExpression> columnList = new List<ColumnExpression>(SystemUser.Columns);
            columnList.Remove(SystemUser._.UserID);
            
            result = this.dc.Update<SystemUser>(userID, user, columnList.ToArray());

            result = this.dc.Update<SystemUser>(user, SystemUser._.UserID == userID);
            result = this.dc.Update<SystemUser>(user, SystemUser._.UserID == userID, SystemUser.Columns);
            result = this.dc.Update<SystemUser>(user, SystemUser._.UserID == userID, SystemUser.Except(SystemUser._.UserID));

            result = this.dc.Delete<SystemUser>(SystemUser._.UserID == userID);
            Assert.AreEqual(1, result);
        }

        [Test]
        public void Test22()
        {
            string key1 = System.Guid.NewGuid().ToString();
            string key2= System.Guid.NewGuid().ToString();
            MutipleKeysTable mt = new MutipleKeysTable();
            mt.Key1 = key1;
            mt.Key2 = key2;
            mt.Value1 = "mt value1";
            mt.Value2 = "mt value2";
            int result = this.dc.Insert<MutipleKeysTable>(mt);
            Assert.AreEqual(1, result);

            System.Data.DataTable dataTabe = this.dc.QueryForDataTable<MutipleKeysTable>();
            Assert.IsNotNull(dataTabe);
            Assert.IsTrue(dataTabe.Rows.Count > 0);
            Assert.AreEqual(2, dataTabe.PrimaryKey.Length);
            Assert.AreEqual("Key1", dataTabe.PrimaryKey[0].ColumnName);
            Assert.AreEqual("Key2", dataTabe.PrimaryKey[1].ColumnName);

            MutipleKeysTable mt2 = this.dc.GetData<MutipleKeysTable>(new object[] { key1, key2 });
            Assert.AreEqual(key1, mt2.Key1);
            Assert.AreEqual(key2, mt2.Key2);
            Assert.AreEqual(mt.Value1, mt2.Value1);
            Assert.AreEqual(mt.Value2, mt2.Value2);

            MutipleKeysTable mt3 = this.dc.GetData<MutipleKeysTable>(new object[] { key1, key2 }, MutipleKeysTable.Except(MutipleKeysTable._.Value2));
            Assert.AreEqual(key1, mt3.Key1);
            Assert.AreEqual(key2, mt3.Key2);
            Assert.AreEqual(mt.Value1, mt3.Value1);
            Assert.IsTrue(string.IsNullOrEmpty(mt3.Value2));

            string newKey1 = System.Guid.NewGuid().ToString();
            mt.Key1 = newKey1;
            result = this.dc.Update<MutipleKeysTable>(new object[] { key1, key2 }, mt, MutipleKeysTable._.Key1);
            Assert.AreEqual(1, result);

            bool exists = this.dc.Exists<MutipleKeysTable>(key1, key2);
            Assert.AreEqual(false, exists);

            MutipleKeysTable mt4 = this.dc.GetData<MutipleKeysTable>(MutipleKeysTable._.Key1 == newKey1 & MutipleKeysTable._.Key2 == key2, MutipleKeysTable.Except(MutipleKeysTable._.Value1));
            Assert.IsNotNull(mt4);
            Assert.AreEqual(newKey1, mt4.Key1);
            Assert.AreEqual(key2, mt4.Key2);
            Assert.IsTrue(string.IsNullOrEmpty(mt4.Value1));
            Assert.AreEqual(mt.Value2, mt4.Value2);

            MutipleKeysTable mt5 = new MutipleKeysTable();
            mt5.Key1 = System.Guid.NewGuid().ToString();
            mt5.Key2 = System.Guid.NewGuid().ToString();
            mt5.Value1 = System.Guid.NewGuid().ToString();
            result = this.dc.Save<MutipleKeysTable>(mt5, MutipleKeysTable.Except(MutipleKeysTable._.Value2));
            Assert.AreEqual(1, result);
            result = this.dc.Delete<MutipleKeysTable>(mt5);
            Assert.AreEqual(1, result);

            result = this.dc.Delete<MutipleKeysTable>(newKey1, key2);
            Assert.AreEqual(1, result);
        }

        [Test]
        public void Test23()
        {
            CommonCommandBuilder commonCommandBuilder = (this.dc.Provider as DataProvider).DataObjectAdapters[typeof(Issue)].CommandBuilder.CommonCommandBuilder;
            CommonCommand c1 = new CommonCommand();
            c1.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID]'";
            CommonCommand c1r = commonCommandBuilder.GetMappingCommand(c1);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID]'", c1r.CommandText);

            CommonCommand c2 = new CommonCommand();
            c2.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID] '''";
            CommonCommand c2r = commonCommandBuilder.GetMappingCommand(c2);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID] '''", c2r.CommandText);

            CommonCommand c3 = new CommonCommand();
            c3.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID] '' \"'";
            CommonCommand c3r = commonCommandBuilder.GetMappingCommand(c3);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID] '' \"'", c3r.CommandText);

            CommonCommand c4 = new CommonCommand();
            c4.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID] '' \"' OR [Title] == ' [Title] '' [IssueID] '";
            CommonCommand c4r = commonCommandBuilder.GetMappingCommand(c4);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID] '' \"' OR ISSUE_TITLE == ' [Title] '' [IssueID] '", c4r.CommandText);

            CommonCommand c5 = new CommonCommand();
            c5.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID] '' \"' OR [Title] == ' [Title] '' [IssueID] \" [Locked] \"'";
            CommonCommand c5r = commonCommandBuilder.GetMappingCommand(c5);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID] '' \"' OR ISSUE_TITLE == ' [Title] '' [IssueID] \" [Locked] \"'", c5r.CommandText);

            CommonCommand c6 = new CommonCommand();
            c6.CommandText = "SELECT * FROM .[Issue] WHERE [Title] == '[IssueID] '' \"' OR [Title] == ' [Title] '' [IssueID] \" [Locked] \"' AND '' = ''''''";
            CommonCommand c6r = commonCommandBuilder.GetMappingCommand(c6);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE WHERE ISSUE_TITLE == '[IssueID] '' \"' OR ISSUE_TITLE == ' [Title] '' [IssueID] \" [Locked] \"' AND '' = ''''''", c6r.CommandText);
        }

        [Test]
        public void Test24()
        {
            (this.dc.Provider as DataProvider).GetDataObjectAdapter(typeof(IssueA)).UseDynamicProxy = false;
            (this.dc.Provider as DataProvider).GetDataObjectAdapter(typeof(LineA)).UseDynamicProxy = false;
            (this.dc.Provider as DataProvider).GetDataObjectAdapter(typeof(MutipleKeysTableA)).UseDynamicProxy = false;
            (this.dc.Provider as DataProvider).GetDataObjectAdapter(typeof(SystemUserA)).UseDynamicProxy = false;

            IssueA issue = new IssueA();
            issue.CreateTime = System.DateTime.Now;
            issue.Description = "Issue for UnitTest.";
            issue.IssueType = IssueType.Issue;
            issue.Locked = false;
            issue.Owner = "sa";
            issue.Progress = 10;
            issue.Status = IssueStatus.New;
            issue.Title = ISSUETITLE_FORTEST;
            this.dc.Insert<IssueA>(issue);

            int maxID = Convert.ToInt32(this.dc.GetMax<IssueA>(IssueA._.IssueID));
            Assert.AreEqual(1, this.dc.GetCount<IssueA>(IssueA._.IssueID == maxID));
            issue.IssueID = maxID;
            issue.Description = "test again";
            this.dc.Update<IssueA>(issue);

            issue = this.dc.GetData<IssueA>(issue.IssueID);
            Assert.AreEqual("test again", issue.Description);

            IList<IssueA> issueList = this.dc.QueryForList<IssueA>();
            Assert.IsNotNull(issueList);
            Assert.IsNotEmpty(issueList as ICollection);

            issueList = this.dc.QueryForList<IssueA>(IssueA._.Title == ISSUETITLE_FORTEST & IssueA._.IssueID > 0);
            Assert.IsNotNull(issueList);


            this.dc.Delete<IssueA>(issue);
            Assert.AreEqual(0, this.dc.GetCount<IssueA>(IssueA._.IssueID == maxID));
        }

        [Test]
        public void Test25()
        {
            DataContext dataContext = this.dc.DatabaseType == DatabaseType.OTHER ? new DataContext(this.dc.ProviderName, this.dc.Connection.ConnectionString) : new DataContext(this.dc.DatabaseType, this.dc.Connection.ConnectionString);
            dataContext.UseDynamicProxy = false;

            IssueA issue = new IssueA();
            issue.CreateTime = System.DateTime.Now;
            issue.Description = "Issue for UnitTest.";
            issue.IssueType = IssueType.Issue;
            issue.Locked = false;
            issue.Owner = "sa";
            issue.Progress = 10;
            issue.Status = IssueStatus.New;
            issue.Title = ISSUETITLE_FORTEST;
            dataContext.Insert<IssueA>(issue);

            int maxID = Convert.ToInt32(dataContext.GetMax<IssueA>(IssueA._.IssueID));
            Assert.AreEqual(1, dataContext.GetCount<IssueA>(IssueA._.IssueID == maxID));
            issue.IssueID = maxID;
            issue.Description = "test again";
            dataContext.Update<IssueA>(issue);

            issue = dataContext.GetData<IssueA>(issue.IssueID);
            Assert.AreEqual("test again", issue.Description);

            IList<IssueA> issueList = dataContext.QueryForList<IssueA>();
            Assert.IsNotNull(issueList);
            Assert.IsNotEmpty(issueList as ICollection);

            issueList = dataContext.QueryForList<IssueA>(IssueA._.Title == ISSUETITLE_FORTEST & IssueA._.IssueID > 0);
            Assert.IsNotNull(issueList);


            dataContext.Delete<IssueA>(issue);
            Assert.AreEqual(0, dataContext.GetCount<IssueA>(IssueA._.IssueID == maxID));
        }

        [Test]
        public void Test26()
        {
            int dataCount = this.dc.GetCount<IssueA>();
            int maxID = (Convert.ToInt32(this.dc.GetMax<IssueA>(IssueA._.IssueID)));
            
            IList<IssueA> issueListForTest = new List<IssueA>();
            for (int i = 0; i < 5; i++)
            {
                IssueA issue1 = new IssueA();
                issue1.CreateTime = System.DateTime.Now;
                issue1.Description = "Issue for UnitTest.";
                issue1.IssueType = IssueType.Issue;
                issue1.Locked = false;
                issue1.Owner = "sa";
                issue1.Progress = i;
                issue1.Status = IssueStatus.New;
                issue1.Title = ISSUETITLE_FORTEST;
                issueListForTest.Add(issue1);
            }

            int count = this.dc.Insert<IssueA>(issueListForTest);
            Assert.AreEqual(5, count);
            Assert.AreEqual(dataCount + 5, this.dc.GetCount<IssueA>());

            int originalCount = this.dc.GetCount<IssueA>(IssueA._.Description.IsNull());
            count = this.dc.Insert<IssueA>(issueListForTest, IssueA.Except(IssueA._.Description));
            Assert.AreEqual(5, count);
            Assert.AreEqual(originalCount + 5, this.dc.GetCount<IssueA>(IssueA._.Description.IsNull()));

            Assert.AreEqual(this.dc.GetCount<IssueA>() - this.dc.GetCount<IssueA>(IssueA._.Description.IsNull()), this.dc.GetCount<IssueA>(IssueA._.Description.IsNotNull()));
            
            IList<IssueA> list = this.dc.QueryForList<IssueA>(IssueA._.Description == issueListForTest[0].Description);
            foreach (IssueA ia in list)
            {
                ia.Status = IssueStatus.Fixed;
            }
            this.dc.Update<IssueA>(list);
            IList<IssueA> l2 = this.dc.QueryForList<IssueA>(IssueA._.Description == issueListForTest[0].Description);
            foreach (IssueA ia in l2)
            {
                Assert.AreEqual(false, ia.Locked);
                Assert.AreEqual(IssueStatus.Fixed, ia.Status);

                ia.Status = IssueStatus.Close;
                ia.Locked = true;
            }

            this.dc.Update<IssueA>(l2, IssueA.Except(IssueA._.Status));
            IList<IssueA> l3 = this.dc.QueryForList<IssueA>(IssueA._.Description == issueListForTest[0].Description);
            foreach (IssueA ia in l3)
            {
                Assert.AreEqual(IssueStatus.Fixed, ia.Status);
                Assert.AreEqual(true, ia.Locked);
            }

            IssueA issue2 = new IssueA();
            issue2.CreateTime = System.DateTime.Now;
            issue2.Description = "Issue for UnitTest.";
            issue2.IssueType = IssueType.Issue;
            issue2.Locked = false;
            issue2.Owner = "sa";
            issue2.Progress = 65;
            issue2.Status = IssueStatus.New;
            issue2.Title = ISSUETITLE_FORTEST;
            l3.Add(issue2);
            this.dc.Save<IssueA>(l3);
            IList<IssueA> l4 = this.dc.QueryForList<IssueA>(IssueA._.Description == issueListForTest[0].Description);
            Assert.AreEqual(l3.Count, l4.Count);

            l4[0].Description = "new for testing";
            this.dc.Save<IssueA>(l4, IssueA.Except(IssueA._.Description, IssueA._.Locked));
            IList<IssueA> l5 = this.dc.QueryForList<IssueA>(IssueA._.Description == issueListForTest[0].Description);
            Assert.AreEqual(l4.Count, l5.Count);

            IssueA issue3 = new IssueA();
            issue3.CreateTime = System.DateTime.Now;
            issue3.Description = "Issue for UnitTest.";
            issue3.IssueType = IssueType.Issue;
            issue3.Locked = false;
            issue3.Owner = "sa";
            issue3.Progress = 67;
            issue3.Status = IssueStatus.New;
            issue3.Title = ISSUETITLE_FORTEST;
            l5.Add(issue3);

            int existsCount = this.dc.Exists<IssueA>(l5);
            Assert.AreEqual(l4.Count, existsCount);

            int deletedCount = this.dc.Delete<IssueA>(l5);
            Assert.AreEqual(l4.Count, deletedCount);

            Assert.AreEqual(0, this.dc.GetCount<IssueA>(IssueA._.Description == issueListForTest[0].Description));
            
            deletedCount = this.dc.Delete<IssueA>(IssueA._.IssueID > maxID);
            Assert.AreEqual(5, deletedCount);
        }

        [Test]
        public void Test27()
        {
            IList<IssueA> issueList = this.dc.QueryForList<IssueA>(IssueA.All ^ IssueA._.IssueID);
            Assert.IsNotNull(issueList);            
            Assert.IsNotEmpty(issueList as ICollection);

            //IList<IssueA> issueList2 = this.dc.ExecuteForList<IssueA>(new CommonCommand("select * from dac_issue order by issue_id"));
			IList<IssueA> issueList2 = this.dc.ExecuteForList<IssueA>(new CommonCommand("select * from DAC_ISSUE order by ISSUE_ID"));
            Assert.IsNotNull(issueList2);
            Assert.IsNotEmpty(issueList2 as ICollection);

            Assert.AreEqual(issueList.Count, issueList2.Count);
        }

        [Test]
        public void Test28()
        {
            DataContext dataContext = this.dc.DatabaseType == DatabaseType.OTHER ? new DataContext(this.dc.ProviderName, this.dc.Connection.ConnectionString) : new DataContext(this.dc.DatabaseType, this.dc.Connection.ConnectionString);
            dataContext.UseDynamicProxy = false;
            dataContext.Log = System.Console.Out;
            
            string command = "select * from dac_issue";
            IList<IssueA> issueList = dataContext.ExecuteForList<IssueA>(new CommonCommand(command));

            Assert.IsNotNull(issueList);
            Assert.IsNotEmpty(issueList as ICollection);
        }

        [Test]
        public void Test29()
        {
            IList<IssueA> il = this.dc.QueryForList<IssueA>(_IssueA._Title.In(new string[] { "Issue Title for UnitTest", "b", "c" }));
            Assert.IsNotNull(il);
            Assert.IsNotEmpty(il as ICollection);

            IList<IssueA> il2 = this.dc.QueryForList<IssueA>(_IssueA._IssueID.In(new int[] { Convert.ToInt32(this.dc.GetMax<IssueA>(IssueA._.IssueID)), 2, 3 }));
            Assert.IsNotNull(il2);
            Assert.IsNotEmpty(il2 as ICollection);
        }


        [Test]
        public void Test30()
        {
            DataContext dataContext = this.dc.DatabaseType == DatabaseType.OTHER ? new DataContext(this.dc.ProviderName, this.dc.Connection.ConnectionString) : new DataContext(this.dc.DatabaseType, this.dc.Connection.ConnectionString);
            dataContext.Log = new LogMethodWrapper(this.WriteLog);

            Log log = new Log();
            log.LogId = 1;
            log.UserId = "test";
            log.ActionDesc = "desc";
            log.ActionType = "type";
            log.ActionTime = DateTime.Now;
            dataContext.Save<Log>(log);

            DateTime begin, end;
            begin = DateTime.Now.AddDays(-1);
            end = DateTime.Now;
            IList<Log> logList = dataContext.QueryForList<Log>(_Log._ActionTime.Between(begin, end) & new ValueExpression(begin) <= end);

            Assert.IsNotNull(logList);
            Assert.IsNotEmpty(logList as ICollection);
            Assert.GreaterOrEqual(logList.Count, 1);

            logList = dataContext.QueryForList<Log>(_Log._ActionTime.Between(begin, end) & new ValueExpression(end) <= begin);
 
            Assert.IsEmpty(logList as ICollection);


            dataContext.Delete<Log>(log);
        }


        public void WriteLog(object value)
        {
            System.Console.Write(value);
            //System.Diagnostics.Debug.Write(value);
        }

        [Test]
        public void Test31()
        {
            RaisingStudio.Data.CommonCommand select = new CommonCommand();
            select.CommandText = "SELECT ISSUE_ID FROM DAC_ISSUE";
            // TODO: execute for list to basic types.
            IList<int> idList = this.dc.ExecuteForList<int>(select);
            Assert.IsNotEmpty(idList as ICollection);

            IList<int> idListF = this.dc.ExecuteForList<int>(select, 1, 2);
            Assert.AreEqual((int)idListF[0], idList[1]);
            Assert.LessOrEqual(idListF.Count, 2);

            List<int> idList2 = new List<int>();
            foreach (int i in this.dc.ExecuteQuery<int>(select))
            {
                idList2.Add(i);
            }
            Assert.IsNotEmpty(idList2 as ICollection);
            Assert.AreEqual(idList2.Count, idList.Count);

            List<int> idListF2 = new List<int>();
            foreach (int i in this.dc.ExecuteQuery<int>(select, 1, 2))
            {
                idListF2.Add(i);
            }
            Assert.AreEqual((int)idListF2[0], idList[1]);
            Assert.LessOrEqual(idListF2.Count, 2);
        }

        [Test]
        public void Test32()
        {
            string userID = System.Guid.NewGuid().ToString();

            SystemUserB user = new SystemUserB();
            user.UserID = userID;
            user.UserName = "user name";
            user.FullName = "full name";
            user.ChangingPassword = false;
            user.UserDescription = "user description";
                        
            int result = this.dc.Save<SystemUserB>(user);
            Assert.AreEqual(1, result);

            result = this.dc.Update<SystemUserB>(userID, user);
            result = this.dc.Update<SystemUserB>(userID, user, SystemUserB._);

            List<ColumnExpression> columnList = SystemUserB._;
            columnList.Remove(SystemUserB._.UserID);

            result = this.dc.Update<SystemUserB>(userID, user, columnList.ToArray());

            result = this.dc.Update<SystemUserB>(user, SystemUserB._.UserID == userID);
            result = this.dc.Update<SystemUserB>(user, SystemUserB._.UserID == userID, SystemUserB._);
            result = this.dc.Update<SystemUserB>(user, SystemUserB._.UserID == userID, SystemUserB._.Except(SystemUserB._.UserID));

            IList<SystemUserB> list = this.dc.QueryForList<SystemUserB>(SystemUserB._, SystemUserB._);
            list = this.dc.QueryForList<SystemUserB>(SystemUserB._.UserID > string.Empty ^ SystemUserB._.UserID, SystemUserB._.UserID);
            list = this.dc.QueryForList<SystemUserB>(SystemUserB._, 0, 10);

            list = this.dc.QueryForList<SystemUserB>(SystemUserB._.OrderBy(SystemUserB._.UserID));
            list = this.dc.QueryForList<SystemUserB>(SystemUserB._.Where(SystemUserB._.UserID > string.Empty).OrderBy(SystemUserB._.UserID, SortingDirection.Descending));
            list = this.dc.QueryForList<SystemUserB>((SystemUserB._.UserID > string.Empty) ^ !SystemUserB._.UserID);
            list = this.dc.QueryForList<SystemUserB>((SystemUserB._.UserID > string.Empty) ^ !SystemUserB._.UserID ^ SystemUserB._.UserName);

            result = this.dc.Delete<SystemUserB>(SystemUserB._.UserID == userID);
            Assert.AreEqual(1, result);
        }

        [Test]
        public void Test33()
        {
            string userID = System.Guid.NewGuid().ToString();

            SystemUserB user = new SystemUserB();
            user.UserID = userID;
            user.UserName = "user name";
            user.FullName = "full name";
            user.ChangingPassword = false;
            user.UserDescription = "user description";

            int result = this.dc.Save<SystemUserB>(user);
            Assert.AreEqual(1, result);

            IList<SystemUserB> list = this.dc.QueryForList<SystemUserB>(SystemUserB._, SystemUserB._);
            SystemUserB u = (list as List<SystemUserB>).Find(delegate(SystemUserB s) { return s.FullName == "full name"; });

            List<SystemUserB> l = new List<SystemUserB>(list);
            SystemUserB u2 = l.Find(delegate(SystemUserB s) { return s.FullName == "full name"; });

        }
    }
}
